home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4417 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: Rezonet.net!news
  2. From: ray@ultimate-tech.com (Ray Dunn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursive function -> how do you exit one?
  5. Date: 2 Feb 1996 18:38:32 GMT
  6. Organization: Ultimate Technographics Inc.
  7. Message-ID: <4etln8$63d@ns.RezoNet.NET>
  8. References: <4eh1g8$aba@pulp.ucs.ualberta.ca> <310d27a6.171790522@news.demon.co.uk> <4elvt9$ood@news.bellglobal.com>
  9. NNTP-Posting-Host: 204.19.230.7
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In referenced article, Steve Tupy says...
  15. >: You need to return a value which makes all the recursed functions
  16. >: exit without further processing.
  17. >
  18. >: if the function returns an int then you could return -1 as aborted.
  19. >: if it returns a char or char* you can use a (first) character that
  20. >: cannot exist in your string. eg 0xff
  21. >
  22. >: char* foo()
  23. >: {
  24. >:    result=foo();
  25. >:    if(*result!=0xff)
  26. >:    {
  27. >:       // process things here //
  28. >:       if (aborted) return "\xff";
  29. >:    }
  30. >:    return result;
  31. >: }
  32. >
  33. >: Does that help?
  34. >
  35. >        What is result, an int? Where is it defined? Don't you agree 
  36. >that it should also be static? If not, you might find that its value 
  37. >gets lost on the stack, especially when you are unwinding the stack 
  38. >during a recursive call...
  39.  
  40. The declaration of result has certainly been ommitted here, and because 
  41. it is getting the result of foo(), it should be a  char *;  A more 
  42. natural thing to return in the aborted state would be NULL.
  43.  
  44. But in any case, there is absolutely no need for result to be static, 
  45. as on every return, result is being reupdated with the current result 
  46. of foo.
  47.  
  48. --
  49. Ray Dunn (opinions are my own) | Phone: (514) 938 9050
  50. Montreal                       | Phax : (514) 938 5225
  51. ray@ultimate-tech.com          | Home : (514) 630 3749
  52.  
  53.